<?php
require 'data/data.php';

session_start();

// 1. 退出操作后，清除自动登录令牌

$isLogout = false;

if (isset($_SESSION['logout']) && $_SESSION['logout'] === true ) {
    $isLogout = true;
    setcookie('remember-token', '', time() - 3600);
    unset($_SESSION['logout']);
}

// 2. 判断当前用户是否登录

$isSignin = isset($_SESSION['user']) && !empty($_SESSION['user']) ? true : false;

$user = $isSignin ? $_SESSION['user'] : null;

// 3. 自动登录

if (isset($_COOKIE['remember-token']) && !empty($_COOKIE['remember-token']) && !$isLogout) {

    if (!$isSignin) {
        $userId = $_COOKIE['user-id'];
        $rememberToken = $_COOKIE['remember-token'];
        $sql = 'select id,user_name,password,remember_token, token_salt from users where id=?';
        $data = query($sql, [$userId]);
        $user = $data[0];
        $salt = $user['token_salt'];
        $seToken = hash('sha256', $rememberToken . $salt);

        // 比对加密后的自动登录令牌

        if ($seToken === $user['remember_token']) {

            $_SESSION['user'] = $user;
            $isSignin = true;
        }
    }
}

// 4. 取出一次性的错误包信息

$hasError = isset($_SESSION['has_error']) ? true : false;

$errorBag = isset($_SESSION['error_bag']) ? $_SESSION['error_bag'] : ['error' => ''];
// 5. 清除session中的一次性错误信息

if (isset($_SESSION['has_error']) && isset($_SESSION['error_bag'])) {

   unset($_SESSION['has_error']);
   unset($_SESSION['error_bag']);
}

// 6. 判断用户登录时是否勾选了记住我

if (isset($_SESSION['remember-token']) && !empty($_SESSION['remember-token'])) {

    $rememberToken = $_SESSION['remember-token'];
    $userId = $user['id'];
    unset($_SESSION['remember-token']);
    // 把remember-token写入cookie

    setcookie('remember-token', $rememberToken, time() + 3600 * 24 * 3);

    setcookie('user-id', $userId, time() + 3600 * 24 * 3);
}

$goods = query('select * from goods');

$countOfCart = 0;
if (isset($_SESSION['cart']) && !empty($_SESSION['cart'])) {
    $cart = $_SESSION['cart'];
    $countOfCart = count($cart);
}
?>
<!DOCTYPE html>
<html>
<head>
    <meta charset=UTF-8"/>
    <title>凡客</title>
    <link rel="stylesheet" type="text/css" href="public/css/vancl.css"/>
</head>
<body>

<!--顶部开始-->
<div id="top">
    <div class="top_container">
        <div class="top_right">

            <!--已登录-->
        <?php if($isSignin): ?>
            <span class="sp1">
                您好,<span class="username"><?php echo $user['user_name']?></span>!
            </span>
            <a href="action/logout_action.php" class="login">退出登录</a>
            <span class="sp2">|</span>
            <a href="" class="reg">更换用户</a>
            <!--已登录-->
        <?php else: ?>
            <!--未登录-->
            <span class="sp1">
      您好,<span style="color: #f00;"></span> 欢迎光临凡客诚品!
    </span>
            <a href="signin.php" class="login">登录</a>

            <span class="sp2">|</span>

            <a href="register.php" class="reg">注册</a>
            <!--未登录-->
        <?php endif; ?>
            <a href="javascript:void(0);" class="order">我的订单</a>

            <img src="public/img/notice.png"/ class="notice">

            <a href="" class="webgg">网站公告</a>

            <img src="public/img/w1.jpg"/ class="w1">

            <img src="public/img/w2.jpg"/ class="w2">

        </div>

    </div>
</div>
<!--顶部结束-->


<!--头部搜索开始-->
<div id="search">
    <div class="search_area">

        <div class="area_top">
            <input type="text" class="search_text" placeholder="搜“羊毛大衣”，体验与众不同"/>
            <div class="search_button"></div>
            <div class="search_shoppingcar"><a href="cart.php"><span class="search_shoppingcar_word">购物车(<?php echo $countOfCart ?>)</span></a>
            </div>
        </div>

        <div class="area_bottom">
            <span class="hot_search">热门搜索：</span>
            <a href="" class="hot_search1">T恤</a>
            <a href="">免烫衬衫</a>
            <a href="">黑标</a>
            <a href="">羊毛大衣</a>
            <a href="">休闲裤</a>
            <a href="">户外鞋</a>
            <a href="">袜品</a>
        </div>
    </div>
</div>
<!--头部搜索结束-->
<!--头部导航栏开始-->
<div id="head_nav">
    <img src="public/img/vancl_logo.png" class="logo"/>
    <a href="" class="nav1">首页</a>
    <a>|</a>
    <a href="" class="nav2">T恤</a>
    <a>|</a>
    <a href="" class="nav3">衬衫</a>
    <a>|</a>
    <a href="" class="nav4">黑标</a>
    <a>|</a>
    <a href="" class="nav5">外套</a>
    <a>|</a>
    <a href="" class="nav6">水柔棉</a>
    <a>|</a>
    <a href="" class="nav7">裤装</a>
    <a>|</a>
    <a href="" class="nav8">鞋</a>
    <a>|</a>
    <a href="" class="nav9">家居配饰</a>
    <a>|</a>
    <a href="" class="nav10">韩国购</a>

</div>
<!--头部导航栏结束-->

<!--商品区域开始-->
<div id="spring_new">
    <p>春季新品预售</p>
  <?php foreach ($goods as $g): ?>
    <div class="good">
        <img src="<?php echo $g['image'] ?>" alt=""/>
        <p class="gname"><?php echo $g['name']?></p>
        <p class="price">¥<?php echo $g['price']?></p>
        <a href="action/add_cart_action.php?id=<?php echo $g['id'] ?>">加入购物车</a>
    </div>
  <?php endforeach; ?>  
</div>
<!--商品区域结束-->

<!--底部售后-->
<div id="final">
    <div class="area1">
        <div class="sh1"><img src="public/img/sh1.png"/></div>
        <div class="sh2"><img src="public/img/sh2.png"/></div>
        <div class="sh3"><img src="public/img/sh3.png"/ class><p>扫描下载<span class="span1">凡客</span>客户端
            <p>
        </div>
    </div>
    <div class="area2">
        <div class="d1"><a href="">关于凡客</a></div>
        <div class="d2"><a href="">新手指南</a></div>
        <div class="d3"><a href="">配送范围及时间</a></div>
        <div class="d4"><a href="">支付方式</a></div>
        <div class="d5"><a href="">售后服务</a></div>
        <div class="d6"><a href="">帮助中心</a></div>
    </div>
</div>
<!--底部售后结束-->


<!--尾部-->
<div id="tail">
    <p>Copyright 2007 - 2016 vancl.com All Rights Reserved 京ICP证100557号 京公网安备11011502002400号 出版物经营许可证新出发京批字第直110138号</p>
    <p>凡客诚品（北京）科技有限公司</p>
    <ul>
        <li>
            <div class="t1"></div>
        </li>
        <li>
            <div class="t2"></div>
        </li>
        <li>
            <div class="t3"></div>
        </li>
        <li>
            <div class="t4"></div>
        </li>
        <li>
            <div class="t5"></div>
        </li>
        <li>
            <div class="t6"></div>
        </li>
    </ul>
</div>
<!--尾部结束-->
</body>
</html>